/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.ui.edge;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.worlds.AssetManager;
import net.worlds.WorldsException;
import net.worlds.EditorView;
import net.worlds.Main;
import net.worlds.PropertyDef;
import net.worlds.Util;
import net.worlds.World;
import net.worlds.component.InsertObjectDialog;
import net.worlds.component.ManageWorldsFrame;
import net.worlds.component.SaveGameDialog;
import net.worlds.component.SettingsFrame;
import net.worlds.component.WebModelDialog;
import net.worlds.object.GameObject;
import net.worlds.object.Part;
import net.worlds.object.Prop;
import net.worlds.ui.UI;


public final class EditorTop extends EMain{
    public EStickyFrame helpFrame;

    public EButton addRemovePlayerItem;
    public EButton showHideToolsItem;
    public EButton showHideConsoleItem;

    public EMenu fileMenu;
    public EMenu editMenu;
    public EMenu viewMenu;
    public EMenu worldMenu;
    public EMenu arrangeMenu;
    public EMenu toolsMenu;
    
    public EToolbar toolbar;

    public EMenuItem newItem;
    public EMenuItem openItem;
    public EMenuItem importWorldsItem;
    public EMenuItem saveItem;
    public EMenuItem webSaveItem;
    public EMenuItem modelSaveItem;

    public EMenuItem insertObjectItem;
    public EMenuItem insertFileModelItem;
    public EMenuItem insertWebModelItem;
    public EMenuItem copyItem;
    public EMenuItem cutItem;
    public EMenuItem pasteItem;
    public EMenuItem duplicateItem;
    public EMenuItem hingeItem;

    public EMenuItem resetCameraItem;

    private ArrayList<EMenuItem> worldItems = new ArrayList<EMenuItem>(32);

    public EMenuItem moveFrontItem;
    public EMenuItem moveBackItem;
    public EMenuItem layer0Item;
    public EMenuItem layer1Item;
    public EMenuItem layer2Item;
    public EMenuItem groupItem;

    public EMenuItem settingsItem;
    public EMenuItem removePlayerItem;
    public EMenuItem startServerItem;
    public EMenuItem startClientItem;

    public EButton newButton;
    public EButton openButton;
    public EButton saveButton;
    public EButton copyButton;
    public EButton cutButton;
    public EButton pasteButton;
    public EButton groupButton;
    public EButton cursorButton;
    public EButton scaleButton;
    public EButton grabButton;
    public EButton rotateButton;
    public EButton boxButton;
    public EButton circleButton;
    public EButton triButton;
    public EButton hingeButton;
    public EButton ropeButton;
    public EButton weldButton;
    public EButton waterButton;
    public EButton ladderButton;
    public EButton seatButton;
    public EButton terrainButton;
    public EButton playButton;
    public EButton gridButton;
    public EButton snapToGridButton;
    public EButton drawOverButton;
    private EButton undoButton;
    private BufferedImage undoImage;
    private BufferedImage redoImage;
    
    public ESwingFrame tableFrame;
    public ESwingFrame treeFrame;
    public EStickyFrame toolsBackground;
    public ETabFrame toolsTabFrame;
    public EScriptingFrame scriptingFrame;
    public EButton wikiButton;
    public EButton deleteButton;
    public EFrame bottomFrame;

    private EditorTop instance;
    
    public EditorTop(final EditorView view, int width, int height)
    {
        super(width, height);
        instance = this;
        try {
            EMenuBar menuBar = new EMenuBar();
            addChild(2, menuBar);

            fileMenu = menuBar.addMenu("File");
            newItem = fileMenu.addItem("New");
            newItem.setActionListener(KeyEvent.VK_N, new EActionListener()
            {
                public void actionPerformed() {
                    if(Main.loading)
                        return;
                    /*try {
                        Main.newGame();
                        Main.getGame().stop(true);
                        Main.loadFromResource("/default.buildism", true);
                    } catch (IOException ex) {
                        try {
                            Main.newGame();
                            Main.getGame().create();
                        } catch (BuildismException ex2) {
                            Main.error(ex);
                        }
                    }*/
                    Main.getEditorOverlay().commandBar.setVisible(false);
                    Main.getEditorOverlay().showNewFrame();           }
            });
            openItem = fileMenu.addItem("Open Game...");
            openItem.setActionListener(KeyEvent.VK_O, new EActionListener()
            {
                public void actionPerformed() {
                    if(Main.loading)
                        return;
                    UI.doLoad();
                }
            });
            importWorldsItem = fileMenu.addItem("Import Worlds from Game...");
            importWorldsItem.setActionListener(KeyEvent.VK_O, new EActionListener()
            {
                public void actionPerformed() {
                    UI.doImportWorlds();
                }
            });
            saveItem = fileMenu.addItem("Save Game...");
            saveItem.setActionListener(KeyEvent.VK_S, new EActionListener()
            {
                public void actionPerformed() {
                    UI.doSave();
                }
            });
            webSaveItem = fileMenu.addItem("Save to 2DWorlds.org...");
            webSaveItem.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    new SaveGameDialog().setVisible(true);
                }
            });
            webSaveItem = fileMenu.addItem("Save Selection as Model...");
            webSaveItem.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.doSaveModel();
                }
            });
            webSaveItem = fileMenu.addItem("Save Selection to 2DWorlds.org...");
            webSaveItem.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.doSaveModelOnline();
                }
            });
            fileMenu.calculateSize();

            editMenu = menuBar.addMenu("Edit");
            insertObjectItem = editMenu.addItem("Insert Object...");
            insertObjectItem.setActionListener(KeyEvent.VK_I, new EActionListener()
            {
                public void actionPerformed() {
                    new InsertObjectDialog().setVisible(true);
                }
            });
            insertFileModelItem = editMenu.addItem("Insert Model from File...");
            insertFileModelItem.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.doLoadModel();
                }
            });
            insertWebModelItem = editMenu.addItem("Insert Model from Web...");
            insertWebModelItem.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    new WebModelDialog().setVisible(true);
                }
            });
            copyItem = editMenu.addItem("Copy");
            copyItem.setActionListener(KeyEvent.VK_C, new EActionListener()
            {
                public void actionPerformed() {
                    UI.doCopy();
                }
            });
            copyItem.setEnabled(false);
            cutItem = editMenu.addItem("Cut");
            cutItem.setActionListener(KeyEvent.VK_X, new EActionListener()
            {
                public void actionPerformed() {
                    try {
                        UI.doCopy();
                        UI.deleteSelection();
                    } catch (WorldsException ex) {
                        Main.error(ex);
                    }
                }
            });
            cutItem.setEnabled(false);
            pasteItem = editMenu.addItem("Paste");
            pasteItem.setActionListener(KeyEvent.VK_V, new EActionListener()
            {
                public void actionPerformed() {
                    try {
                        UI.doPaste(false);
                    } catch (WorldsException ex) {
                        Main.error(ex);
                    }
                }
            });
            duplicateItem = editMenu.addItem("Duplicate");
            duplicateItem.setActionListener(KeyEvent.VK_D, new EActionListener()
            {
                public void actionPerformed() {
                    try {
                        UI.doCopy();
                        UI.doPaste(false);
                    } catch (WorldsException ex) {
                        Main.error(ex);
                    }
                }
            });
            pasteItem.setEnabled(false);
            hingeItem = editMenu.addItem("Add center hinge");
            hingeItem.setActionListener(new EActionListener()
            {
                @Override
                public void actionPerformed() {
                    if(UI.getNumberSelected() != 1 || !(UI.getSelected().get(0) instanceof Part))
                        Main.log("Please select 1 part");
                    else
                    {
                        if(UI.getNumberSelected() != 1 || !(UI.getSelected().get(0) instanceof Part))
                            Main.log("Please select 1 part");
                        else
                        {
                            try {
                                UI.makeJoint(((Part) UI.getSelected().get(0)).getPosition());
                            } catch (WorldsException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                }
            });
            hingeItem.setEnabled(false);
            editMenu.calculateSize();

            viewMenu = menuBar.addMenu("View");
            resetCameraItem = viewMenu.addItem("Reset Camera");
            resetCameraItem.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.resetCamera();
                }
            });
            viewMenu.calculateSize();
            
            worldMenu = menuBar.addMenu("World");
            worldMenu.calculateSize();

            arrangeMenu = menuBar.addMenu("Arrange");
            moveFrontItem = arrangeMenu.addItem("Move to front");
            moveFrontItem.setActionListener(KeyEvent.VK_BRACELEFT, new EActionListener()
            {
                public void actionPerformed() {
                    if(UI.getNumberSelected() > 0)
                        try {
                            List<GameObject> selected = UI.getSelected();
                            synchronized(selected)
                            {
                                for(GameObject obj : selected)
                                {
                                    if(obj instanceof Prop)
                                        UI.moveToFront((Prop)obj);
                                }
                            }
                        } catch (WorldsException ex) {
                            throw new RuntimeException(ex);
                        }
                    else
                        Main.log("Please select a Part");
                }
            });
            moveFrontItem.setEnabled(false);
            moveBackItem = arrangeMenu.addItem("Move to back");
            moveBackItem.setActionListener(KeyEvent.VK_BRACERIGHT, new EActionListener()
            {
                public void actionPerformed() {
                    if(UI.getNumberSelected() > 0)
                        try {
                            List<GameObject> selected = UI.getSelected();
                            synchronized(selected)
                            {
                                for(GameObject obj : selected)
                                {
                                    if(obj instanceof Prop)
                                        UI.moveToBack((Prop)obj);
                                }
                            }
                        } catch (WorldsException ex) {
                            throw new RuntimeException(ex);
                        }
                    else
                        Main.log("Please select a Part");
                }
            });
            moveBackItem.setEnabled(false);
            layer2Item = arrangeMenu.addItem("Move to foreground");
            layer2Item.setActionListener(KeyEvent.VK_F, new EActionListener()
            {
                @Override
                public void actionPerformed() {
                    UI.moveToLayer(2);
                }
            });
            layer1Item = arrangeMenu.addItem("Move to player layer");
            layer1Item.setActionListener(KeyEvent.VK_B, new EActionListener()
            {
                @Override
                public void actionPerformed() {
                    UI.moveToLayer(1);
                }
            });
            layer0Item = arrangeMenu.addItem("Move to background");
            layer0Item.setActionListener(KeyEvent.VK_H, new EActionListener()
            {
                @Override
                public void actionPerformed() {
                    UI.moveToLayer(0);
                }
            });
            groupItem = arrangeMenu.addItem("Group/Ungroup");
            groupItem.setActionListener(KeyEvent.VK_G, new EActionListener()
            {
                public void actionPerformed() {
                    try {
                        UI.groupSelection();
                    } catch (WorldsException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            });
            groupItem.setEnabled(false);
            arrangeMenu.calculateSize();

            toolsMenu = menuBar.addMenu("Tools");
            settingsItem = toolsMenu.addItem("Settings");
            settingsItem.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    new SettingsFrame().setVisible(true);
                }
            });
            removePlayerItem = toolsMenu.addItem("Remove Player");
            removePlayerItem.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    Main.removePlayer();
                }
            });
            startServerItem = toolsMenu.addItem("Start Server...");
            startServerItem.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.startServer();
                }
            });
            startClientItem = toolsMenu.addItem("Connect to Server...");
            startClientItem.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.startClient();
                }
            });
            toolsMenu.calculateSize();
            
            createToolbar(view, true);

            helpFrame = new EStickyFrame(this, 121, 24, 0, -1, -1, 0);
            helpFrame.setBackgroundColor(new Color(0, 0, 0, 0));
            helpFrame.setUndecorated(true);
            
            EButton helpButton = new EButton("Help!");
            helpButton.setHeight(24);
            helpButton.setWidth(40);
            helpButton.setX(81);
            helpButton.setColor(new Color(90, 90, 90));
            
            EButton chatButton = new EButton("Global Chat");
            chatButton.setHeight(24);
            chatButton.setWidth(80);
            
            undoImage = AssetManager.loadImage("undo.png", "undo.png");
            redoImage = AssetManager.loadImage("redo.png", "redo.png");
            
            undoButton = new EButton(undoImage);
            undoButton.setImage(null);
            undoButton.setTooltip("Undo");
            undoButton.setWidth(24);
            undoButton.setHeight(24);
            undoButton.setX(0);
            undoButton.setColor(new Color(90, 90, 90));
            undoButton.setActionListener(new EActionListener()
            {
                @Override
                public void actionPerformed() {
                    int r = UI.undoRedo();
                    if(r == 1)
                    {
                        undoButton.setImage(undoImage);
                        undoButton.setTooltip("Undo");
                    }
                    else if(r == 0)
                    {
                        undoButton.setImage(redoImage);
                        undoButton.setTooltip("Redo"); 
                    }
                }
            });
            helpFrame.addChild(3, undoButton);
           
            helpFrame.addChild(3, helpButton);
            //helpFrame.addChild(3, chatButton);
            addChild(3, helpFrame);
            
            helpButton.setActionListener(new EActionListener()
            {

                @Override
                public void actionPerformed() {
                    Main.getEditorOverlay().helpFrame.visible = true;
                }
            });
        EButton backButton = null;
        try {
            backButton = new EButton(AssetManager.loadImage("exit.png", "exit.png"));
            backButton.setColor(new Color(90, 90, 90));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        backButton.setHeight(24);
        backButton.setWidth(40);
        backButton.setX(41);
        backButton.setActionListener(new EActionListener()
        {

            @Override
            public void actionPerformed() {
                Main.showMenu();
            }
            
        });
        toolsBackground = new EStickyFrame(this, 220, -1, 0, -1, 55, 0);
        toolsBackground.setUndecorated(true);
        toolsBackground.setBackgroundColor(new Color(192, 192, 192));
        
        treeFrame = new ESwingFrame(toolsBackground, 10, 5, 200, 300, view.getPartsTree());
        toolsTabFrame = new ETabFrame(toolsBackground, 10, 385, 200, 20, 280);
        toolsTabFrame.setLayer(1);
        ESwingFrame propsFrame = new ESwingFrame(this, 0, 0, 0, 0, view.getPropsTable());
        toolsTabFrame.addTab("Basic", propsFrame, new EActionListener()
        {

            @Override
            public void actionPerformed() {
                try {
                    UI.propClass = PropertyDef.CLASS_BASIC;
                    UI.updatePropsTable();
                } catch (WorldsException ex) {
                    ex.printStackTrace();
                }
            }            
        });
        toolsTabFrame.addTab("Advanced", propsFrame, new EActionListener()
        {

            @Override
            public void actionPerformed() {
                try {
                    UI.propClass = PropertyDef.CLASS_ADVANCED;
                    UI.updatePropsTable();
                } catch (WorldsException ex) {
                    ex.printStackTrace();
                }
            }            
        });
        scriptingFrame = new EScriptingFrame(this, 0, 0, 0, 0);
        toolsTabFrame.addTab("Scripting", scriptingFrame);
        toolsTabFrame.setTab("Basic");
        toolsTabFrame.setVisible(false);
        
        wikiButton = new EButton("Look Up on Wiki");
        bottomFrame = new EFrame(toolsBackground, 10, 0, 200, 20)
        {
            @Override
            public void render(Graphics2D g)
            {
                renderChildren(g);
            }
        };
        bottomFrame.setVisible(false);
        try {
            deleteButton = new EButton(AssetManager.loadImage("delete.png", "delete.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        deleteButton.setWidth(20);
        deleteButton.setHeight(20);
        deleteButton.setX(200-20);
        wikiButton.setWidth(200-22);
        wikiButton.setHeight(20);
        deleteButton.setActionListener(new EActionListener()
        {
            @Override
            public void actionPerformed() {
                try {
                    UI.deleteSelection();
                } catch (WorldsException ex) {
                    ex.printStackTrace();
                }
            }
        });
        wikiButton.setActionListener(new EActionListener()
        {

            @Override
            public void actionPerformed() {
                if(UI.getNumberSelected() == 1)
                    Util.wikiLookup(UI.getSelected().get(0).getType());
            }
            
        });
        bottomFrame.addChild(1, wikiButton);
        bottomFrame.addChild(1, deleteButton);
        bottomFrame.setVisible(false);
        
        addChild(0, toolsBackground);
        setSizes();
        //helpFrame.addChild(3, backButton);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
    public void showUndoButton()
    {
        UI.repaintUI = true;
        undoButton.visible = true;
        undoButton.setImage(undoImage);
    }
    public void hideUndoButton()
    {
        undoButton.setImage(null);
        undoButton.visible = false;
    }
    public void createToolbar(final EditorView view, boolean showSideScrollingButtons) throws IOException
    {
            EButtonGroup tools = new EButtonGroup();
            toolbar = new EToolbar(32, 23, 24, 4, 2);
            toolbar.setColors(new Color(62, 67, 77), new Color(62, 67, 77), new Color(200, 200, 200));
            addChild(1, toolbar);
            newButton = toolbar.addButton(AssetManager.loadImage("page_add.png", "page_add.png"));
            newButton.setTooltip("New game");
            newButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    if(Main.loading)
                        return;
                    /*try {
                        Main.newGame();
                        Main.loadFromResource("/default.buildism", true);
                    } catch (IOException ex) {
                        try {
                            Main.newGame();
                            Main.getGame().create();
                        } catch (BuildismException ex2) {
                            Main.error(ex);
                        }
                    }*/
                    Main.getEditorOverlay().commandBar.setVisible(false);
                    Main.getEditorOverlay().showNewFrame();
                }
            });
            openButton = toolbar.addButton(AssetManager.loadImage("folder_go.png", "folder_go.png"));
            openButton.setTooltip("Open saved game");
            openButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.doLoad();
                }
            });
            saveButton = toolbar.addButton(AssetManager.loadImage("page_save.png", "page_save.png"));
            saveButton.setTooltip("Save game");
            saveButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.doSave();
                }
            });
            toolbar.addSeparator();
            copyButton = toolbar.addButton(AssetManager.loadImage("page_copy.png", "page_copy.png"));
            copyButton.setTooltip("Copy selected objects");
            copyButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.doCopy();
                }
            });
            cutButton = toolbar.addButton(AssetManager.loadImage("cut.png", "cut.png"));
            cutButton.setTooltip("Cut selected objects");
            cutButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    try {
                        UI.doCopy();
                        UI.deleteSelection();
                    } catch (WorldsException ex) {
                        Main.error(ex);
                    }
                }
            });
            pasteButton = toolbar.addButton(AssetManager.loadImage("paste_plain.png", "paste_plain.png"));
            pasteButton.setTooltip("Paste objects from clipboard");
            pasteButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    try {
                        UI.doPaste(false);
                    } catch (WorldsException ex) {
                        Main.error(ex);
                    }
                }
            });
            toolbar.addSeparator();
            groupButton = toolbar.addButton(AssetManager.loadImage("group.png", "group.png"));
            groupButton.setTooltip("Group selection");
            groupButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    try {
                        UI.groupSelection();
                    } catch (WorldsException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            });
            toolbar.addSeparator();
            toolbar.setButtonGroup(tools);
            cursorButton = toolbar.addToggleButton(AssetManager.loadImage("cursor.png", "cursor.png"));
            cursorButton.setTooltip("Select");
            cursorButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.setMouseMode(UI.MODE_ARROW);
                    UI.setMouseImage(null);
                    UI.resetCursor();
                    deselect();
                }
            });
            ((EToggleButton)cursorButton).down = true;
            ((EToggleButton)cursorButton).showEdges = true;
            grabButton = toolbar.addToggleButton(AssetManager.loadImage("grab.png", "grab.png"));
            grabButton.setTooltip("Grab");
            grabButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.setMouseMode(UI.MODE_GRAB);
                    UI.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                    UI.setMouseImage(null);
                    deselect();
                }
            });
            scaleButton = toolbar.addToggleButton(AssetManager.loadImage("scale.png", "scale.png"));
            scaleButton.setTooltip("Resize");
            scaleButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.setMouseMode(UI.MODE_SCALE);
                    UI.scaleBox = null;
                    UI.scaleVert = null;
                    UI.setCursor(UI.defaultCursor);
                    UI.setMouseImage("scale");
                    deselect();

                }
            });
            rotateButton = toolbar.addToggleButton(AssetManager.loadImage("rotate.png", "rotate.png"));
            rotateButton.setTooltip("Rotate");
            rotateButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.setMouseMode(UI.MODE_ROTATE);
                    UI.setMouseImage("rotate");
                    UI.resetCursor();
                    deselect();
                }
            });
            boxButton = toolbar.addToggleButton(AssetManager.loadImage("plane.png", "plane.png"));
            boxButton.setTooltip("Draw box");
            boxButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.setMouseMode(UI.MODE_BOX);
                    UI.setMouseImage("plane");
                    UI.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
                    deselect();
                }
            });
            circleButton = toolbar.addToggleButton(AssetManager.loadImage("circle.png", "circle.png"));
            circleButton.setTooltip("Draw circle");
            circleButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.setMouseMode(UI.MODE_CIRCLE);
                    UI.setMouseImage("circle");
                    UI.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
                    deselect();
                }
            });
            triButton = toolbar.addToggleButton(AssetManager.loadImage("triangle.png", "triangle.png"));
            triButton.setTooltip("Draw triangle");
            triButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.setMouseMode(UI.MODE_TRIANGLE    );
                    UI.setMouseImage("triangle");
                    UI.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
                    deselect();
                }
            });
            hingeButton = toolbar.addToggleButton(AssetManager.loadImage("hinge.png", "hinge.png"));
            hingeButton.setTooltip("Place hinge");
            hingeButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.setMouseMode(UI.MODE_HINGE);
                    UI.setMouseImage("hinge");
                    UI.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
                    deselect();
                }
            });
            weldButton = toolbar.addToggleButton(AssetManager.loadImage("link.png", "link.png"));
            weldButton.setTooltip("Place weld");
            weldButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.setMouseMode(UI.MODE_WELD);
                    UI.setMouseImage("link");
                    UI.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
                    deselect();
                }
            });
            ropeButton = toolbar.addToggleButton(AssetManager.loadImage("rope.png", "rope.png"));
            ropeButton.setTooltip("Place rope");
            ropeButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.setMouseMode(UI.MODE_ROPE);
                    UI.setMouseImage("rope");
                    UI.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
                    deselect();
                }
            });
            waterButton = toolbar.addToggleButton(AssetManager.loadImage("water.png", "water.png"));
            waterButton.setTooltip("Draw water");
            waterButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.setMouseMode(UI.MODE_WATER);
                    UI.setMouseImage("water");
                    UI.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
                    deselect();
                }
            });
            if(showSideScrollingButtons)
            {
                ladderButton = toolbar.addToggleButton(AssetManager.loadImage("laddericon.png", "laddericon.png"));
                ladderButton.setTooltip("Draw ladder");
                ladderButton.setActionListener(new EActionListener()
                {
                    public void actionPerformed() {
                        UI.setMouseMode(UI.MODE_LADDER);
                        UI.setCursor(UI.defaultCursor);
                        UI.setMouseImage("laddericon");
                        deselect();
                    }
                });
                seatButton = toolbar.addToggleButton(AssetManager.loadImage("seat.png", "seat.png"));
                seatButton.setTooltip("Add seats");
                seatButton.setActionListener(new EActionListener()
                {
                    public void actionPerformed() {
                        UI.setMouseMode(UI.MODE_SEAT);
                        UI.setCursor(UI.defaultCursor);
                        UI.setMouseImage("seat");
                        deselect();
                    }
                });
                seatButton = toolbar.addToggleButton(AssetManager.loadImage("terrain.png", "terrain.png"));
                seatButton.setTooltip("Change terrain");
                seatButton.setActionListener(new EActionListener()
                {
                    public void actionPerformed() {
                        UI.setMouseMode(UI.MODE_TERRAIN);
                        UI.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
                        UI.setMouseImage("terrain");
                        deselect();
                    }
                });
            }
            toolbar.setButtonGroup(null);
            toolbar.addSeparator();
            playButton = toolbar.addButton(AssetManager.loadImage("stop.png", "stop.png"));
            playButton.setTooltip("Start/stop physics simulation");
            playButton.setActionListener(new EActionListener()
            {
                @Override
                public void actionPerformed() {
                    Main.getGame().toggleIsPlaying();
                }
            });
            toolbar.addSeparator();
            gridButton = toolbar.addToggleButton(AssetManager.loadImage("grid.png", "grid.png"));
            gridButton.setTooltip("Show/hide grid");
            gridButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.showGrid = !UI.showGrid;
                }
            });
            snapToGridButton = toolbar.addToggleButton(AssetManager.loadImage("snaptogrid.png", "snaptogrid.png"));
            snapToGridButton.setTooltip("Enable/disable grid snapping");
            snapToGridButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.snapToGrid = !UI.snapToGrid;
                }
            });
            drawOverButton = toolbar.addToggleButton(AssetManager.loadImage("drawover.png", "drawover.png"));
            drawOverButton.setTooltip("Draw over objects (disable selecting)");
            drawOverButton.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    UI.drawOver = !UI.drawOver;
                }
            });
    }
    public void setSideScrollingButtons(boolean v)
    {
        if(toolbar != null)
            removeChild(toolbar);
        try {
            createToolbar((EditorView) Main.getView(), v);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    private void deselect()
    {
        if(Main.getLocalPlayer() != null)
            Main.getLocalPlayer().deselectTool();
    }
    public void updateWorldMenu()
    {
        for(EMenuItem i : worldItems)
        {
            worldMenu.removeChild(i);
        }
        worldMenu.reset();
        worldItems.clear();
        HashMap<String, World> worlds = Main.getGame().getWorlds();
        for(final String worldName : worlds.keySet())
        {
            EMenuItem item = worldMenu.addItem(worldName);
            item.setActionListener(new EActionListener()
            {
                public void actionPerformed() {
                    Main.getGame().setCurrentWorld(worldName);
                }
            });
            worldItems.add(item);
        }
        EMenuItem manageWorldsItem = worldMenu.addItem("Edit Worlds");
        manageWorldsItem.setActionListener(new EActionListener()
        {
            @Override
            public void actionPerformed() {
                new ManageWorldsFrame().setVisible(true);
            }
        });
        worldItems.add(manageWorldsItem);
        worldMenu.calculateSize();
    }
    
    public void hideAdvancedTools()
    {
        toolsBackground.setVisible(false);
        UI.advancedToolsVisible = false;
        //actionButtonFrame.rightMargin = 0;
    }
    public void showAdvancedTools()
    {
        toolsBackground.setVisible(true);
        UI.advancedToolsVisible = true;
        //actionButtonFrame.rightMargin = 219;
    }
    
    public void setSizes()
    {
        int height = toolsBackground.getHeight();
        int padding = 50;
        int boxHeight = (height - padding)/2;
        treeFrame.setHeight(boxHeight);
        toolsTabFrame.setY(boxHeight+padding/2-10);
        toolsTabFrame.setHeight(boxHeight);
        bottomFrame.setY(boxHeight*2+padding/2);
    }
    
    @Override
    public void setMouseComponent(EComponent c)
    {
        EComponent old = mouseComponent;
        super.setMouseComponent(c);
        if(mouseComponent != old)
            UI.repaintUI = true;
    }
    @Override
    public boolean eMouseUp(int b, int x, int y)
    {
        if(y < 55)
            UI.repaintUI = true;
        return super.eMouseUp(b, x, y);
    }
    @Override
    public boolean eMouseDown(int b, int x, int y)
    {
        if(y < 55)
            UI.repaintUI = true;
        return super.eMouseDown(b, x, y);
    }
    @Override
    public boolean eMouseMove(int x, int y)
    {
        return super.eMouseMove(x, y);
    }
}
